package tgc.edu.yzy.web.admin.controller;

import java.util.HashMap;
import java.util.HashSet;
import java.util.List;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Predicate;
import javax.persistence.criteria.Root;
import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import tgc.edu.yzy.custom.Custom;
import tgc.edu.yzy.custom.DatagridUtils;
import tgc.edu.yzy.entity.Book;
import tgc.edu.yzy.entity.BookRecord;
import tgc.edu.yzy.entity.User;
import tgc.edu.yzy.service.BookRecordService;
import tgc.edu.yzy.service.BookService;
import tgc.edu.yzy.service.UserService;

@Controller
@RequestMapping(value="admin/br")
public class BRController {
	@Autowired
	private BookRecordService brservice;
	@Autowired
	private Custom custom;
	@Autowired
	private BookService bookservice;
	@Autowired
	private UserService userservice;
	
	/***
	 * 跳转至审核图书
	 * @return
	 */
	@RequestMapping(value="index")
	private String index() {
		return "admin/br/index";
	}
	
	@RequestMapping(value="pagelist")
	@ResponseBody
	private Object pagelist(DatagridUtils form) {
		String state="等待处理";
		Pageable pageable=form.buildPageable();
		Page<BookRecord> books=null;
		Specification<BookRecord> spec = new Specification<BookRecord>() {

			@Override
			public Predicate toPredicate(Root<BookRecord> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) {
				HashSet<Predicate> rules = new HashSet<>();
				if(StringUtils.hasText(state)) {
					rules.add(criteriaBuilder.equal(root.get("state"), state));
				}
				return criteriaBuilder.and(rules.toArray(new Predicate[rules.size()]));
			}
		};
		books= brservice.findAll(spec,pageable);
		HashMap<String , Object> result = form.getPageResult(books);
		return result; 
	}
	@RequestMapping(value="ok")
	@ResponseBody
	private String ok(Integer id) {
		BookRecord brs = brservice.findById(id);
		brs.setState("同意借书");
		brservice.save(brs);
		custom.booksrecord(brs.getUser(),brs.getBook(), "同意借书", "图书管理员同意借书");
		
		Book book = brs.getBook();
		Integer loan = book.getLoan();
		loan=loan-1;
		book.setLoan(loan);
		bookservice.save(book);
		
		User userss = brs.getUser();
		Integer number = userss.getNumber();
		number=number+1;
		userss.setNumber(number);
		userservice.save(userss);
		return "";
	}
	
	@RequestMapping(value="no")
	@ResponseBody
	private String no(Integer id) {
		BookRecord brs = brservice.findById(id);
		brs.setState("拒绝借书");
		custom.booksrecord(brs.getUser(),brs.getBook(), "拒绝借书", "图书管理员拒绝借书");
		Book book = brs.getBook();
		Integer state = book.getState();
		state=state+1;
		book.setState(state);
		bookservice.save(book);
		brservice.delete(brs);
		return "";
	}
	/***
	 * 还书
	 */
	@RequestMapping(value="login")
	private String login() {
		return "admin/br/login";
	}
	
	@RequestMapping(value="page")
	@ResponseBody
	private Object page(DatagridUtils form) {
		String state="还书等待处理";
		Pageable pageable=form.buildPageable();
		Page<BookRecord> books=null;
		Specification<BookRecord> spec = new Specification<BookRecord>() {
			@Override
			public Predicate toPredicate(Root<BookRecord> root, CriteriaQuery<?> query, CriteriaBuilder criteriaBuilder) {
				HashSet<Predicate> rules = new HashSet<>();
				if(StringUtils.hasText(state)) {
					rules.add(criteriaBuilder.equal(root.get("state"), state));
				}
				return criteriaBuilder.and(rules.toArray(new Predicate[rules.size()]));
			}
		};
		books= brservice.findAll(spec,pageable);
		HashMap<String , Object> result = form.getPageResult(books);
		return result; 
	}
	
	@RequestMapping(value="oks")
	@ResponseBody
	private String oks(Integer id) {
		//删除图书记录
		BookRecord brs = brservice.findById(id);
		brs.setState("确认已还书");
		brservice.delete(brs);
		//图书修改 新增一本图书
		Book book = brs.getBook();
		Integer loan = book.getLoan();
		loan=loan+1;
		book.setLoan(loan);
		Integer state = book.getState();
		state=state+1;
		book.setState(state);
		bookservice.save(book);
		//用户图书减少一本
		User user = brs.getUser();
		Integer number = user.getNumber();
		number=number-1;
		user.setNumber(number);
		userservice.save(user);
		//借书记录
		custom.booksrecord(brs.getUser(),brs.getBook(), "确认还书", "图书管理员已确认还书");
		return "";
	}
	
	@RequestMapping(value="/userbooks")
	private String userbooks(ModelMap map,Integer id) {
		User users=userservice.findById(id);
		List<BookRecord> books=brservice.findByUserAndState(users, "同意借书");
		map.put("books", books);
		return "admin/br/userbook";
	}
}
